package character;

import Sorts.Books;
import theLibrary.Library;
import theLibrary.scatteredBooks;
import java.util.ArrayList;
import java.util.List;

public class theSystem {
    public static final int AdminMode = 0;
    public static final int UserMode = 1;
    public static final int NobodyMode = 2;

    private int currentMode;
    private Users currentUser;

    public final List<Users> yonghu = new ArrayList<>();
    private final Library library = new Library();

//管理员登录
    public void AdminLogin(){
        currentMode = AdminMode;
        currentUser = null;
    }

//    管理员下线
    public void AdminLogout() {
        currentMode = NobodyMode;
        currentUser = null;
    }

//用户注册
    public void UserZhuCe(String userName,String password) {
        Users users = new Users(userName,password);
        yonghu.add(users);
    }

//用户登录
    public void UserLogin(String userName,String password) {
        for(Users user:yonghu) {
            if(user.getUsername().equals(userName)) {
                currentMode = UserMode;
                currentUser = user;
            }
        }
    }

//用户下线
    public void UserLogout() {
        currentMode = NobodyMode;
        currentUser = null;
    }

//设置用户黑名单
    public void setUserBlack(String userName,String password) {

        if(currentMode!=AdminMode) {
            return;
        }
        for(Users users : yonghu) {
            if(users.getUsername().equals(userName)) {
                users.setBlack();
            }
            if(users.isBlack()) {
                scatteredBooks return_all = new scatteredBooks();
                return_all.addBooks(users.getMyBooks());
                users.returnAll();
            }
        }
    }

//创建书
public void createBook(Books book) {
    if (currentMode!=AdminMode) {
        return;
    }
    scatteredBooks scatteredBooks = new scatteredBooks();

    scatteredBooks.add_Book(book);
}

//查看所有书
    public List<Books> zhaoshushu() {
        if(currentMode != AdminMode) {
            return null;
        }

        List<Books> zhao_shushu = new ArrayList<>();
        int count = 0;

        for(;count < library.getBookSelvesNum(); count++) {
            zhao_shushu.addAll(library.getAllBooksFromA_BookSelf(count));
        }
        return zhao_shushu;
    }

//    查看用户信息
    public List<Users> allusers() {
        if(currentMode!=AdminMode) {
            return null;
        }
        return yonghu;
    }

//给一个书架加书
    public void addBookToBookSelf(Books book, int index) {
        if (currentMode!=AdminMode) {
            return;
        }
        library.addBookToA_BookSelf(book, index);
    }

//    查看一个书架的书
    public List<Books> zhaoshu(int index) {
        if (currentMode != AdminMode) {
            return null;
        }
        return library.getAllBooksFromA_BookSelf(index);
    }

//    书架数量
    public int getshujiashuliang() {
        return library.getBookSelvesNum();
    }

//    用户借书
    public void borrowBook(String name) {
        for (int i = 0; i < library.getBookSelvesNum(); i++) {
            for (Books books : library.getAllBooksFromA_BookSelf(i)) {
                if (books.getName().equals(name)) {
                    if (library.removeA_Book(books,i)) {
                        currentUser.borrowBook(books);
                        return;
                    }
                }
            }
        }
    }

//创建书架
    public void createBookSelf() {
        if(currentMode!=AdminMode) {
            return;
        }
        library.addBookSelf();
    }

}
